#include <QPainter>
#include <QPixmap>
#include "musicInfo.h"

musicInfo::musicInfo(QWidget *parent) : QWidget(parent)
{

    pix.load(":/img/img/musicPic.png");
    pix = pix.scaled((int)(pix.width() * 0.6), (int)(pix.height() * 0.6), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    isHandleSeted = 0;
    handle = NULL;

    title = "unknown";
    author= "unknown";
    special = "unknown";
    fileName = "unknown";



}


void musicInfo::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    QColor color;
    color.setRgb(0, 0, 0, 0);
    painter.fillRect(this->rect(), color);

    QFont font = painter.font();
    font.setPixelSize(20);
    painter.setFont(font);

    QPen pen;
    pen.setColor(QColor(0xFF, 0xFF, 0xFF));
    painter.setPen(pen);

    QRect coverRect(0.5 * this->width() - 0.5 * pix.width() + 40 + 5,
                    30 + 38 - 3, 240, 240);

    QRect musicData(this->width() * 0.5 - 190,
                    this->height() * 0.5 + 95,
                    380, 100);




    painter.drawPixmap(0.5 * this->width() - 0.5 * pix.width(),
                       30, pix);

    //painter.fillRect(coverRect, QColor(0x00, 0x00, 0x00, 0xAA));

    if((isHandleSeted == 1) && ((handle->info.flag & APIC) == APIC))
    {
        int tmpI = handle->info.flag & APIC;
        QPixmap tmp;
        tmp.loadFromData((unsigned char*)handle->info.cover, handle->info.coverSize);
        painter.drawPixmap(coverRect, tmp);
    }
    else
    {
        painter.drawPixmap(coverRect, QPixmap(":/img/img/sampleMusic.png"));
    }

    painter.fillRect(musicData, QColor(0x00, 0x00, 0x00, 0xAA));

#define dataX 4
#define dataY 22
#define wordY 22


    painter.drawText(musicData.x() + dataX, musicData.y() + dataY + wordY * 0, QObject::tr("文件：") + fileName);
    painter.drawText(musicData.x() + dataX, musicData.y() + dataY + wordY * 1, QObject::tr("标题：") + title);
    painter.drawText(musicData.x() + dataX, musicData.y() + dataY + wordY * 2, QObject::tr("作者：") + author);
    //painter.drawText(musicData.x() + dataX, musicData.y() + dataY + wordY * 2, QObject::tr("作曲："));
    painter.drawText(musicData.x() + dataX, musicData.y() + dataY + wordY * 3, QObject::tr("专辑：") + special);
    //painter.drawText(musicData.x() + dataX, musicData.y() + dataY + wordY * 4, QObject::tr("年份："));


#undef dataX
#undef dataY

}


void musicInfo::setHandle(musicHandle *h)
{
    isHandleSeted = 1;
    handle = h;



    if(isHandleSeted == 0)
    {
        title = "unknown";
        author = "unknown";
        special = "unknown";
    }
    else
    {
        //QChar* tmpChar;
        if((handle->info.flag & TIT2) == TIT2)
        {
            if(handle->info.title[0] == 0)
            {
                title = QString::fromLatin1(handle->info.title+1, handle->info.titleSize-1);
            }
            else
            {
                title = QString::fromRawData((QChar *)(handle->info.title+1), (handle->info.titleSize-1) / 2);
                //delete [] tmpChar;

            }

        }
        else
        {
            title = "unknown";
        }

        if((handle->info.flag & TPE1) == TPE1)
        {
            if(handle->info.singer[0] == 0)
            {
                author = QString::fromLatin1(handle->info.singer+1);
            }
            else
            {
                author = QString::fromRawData((QChar*)(handle->info.singer+1), (handle->info.singleSize-1) / 2);
                //delete [] tmpChar;
            }
        }
        else
        {
            author = "unknown";
        }

        if((handle->info.flag & TALB) == TALB)
        {


            char tmpC[handle->info.specialSize];
            memcpy(tmpC, handle->info.special, handle->info.specialSize);

            if(handle->info.special[0] == 0)
            {
                special = QString::fromLatin1(handle->info.special+1);
            }
            else
            {

                special = QString::fromRawData((QChar*)(handle->info.special+1), (handle->info.specialSize-1) / 2);
            ///delete [] tmpChar;
            }
        }
        else
        {
            special = "unknown";
        }
    }
    fileName = QUrl(QString(handle->musicName)).fileName();


    update();
}

void musicInfo::stopHandle()
{
    isHandleSeted = 0;
    handle = NULL;
    title = "unknown";
    author = "unknown";
    special = "unknown";
    fileName = "unknown";

    update();
}
